The use of object-oriented programming techniques can facilitate the development of complex computer programs. Programming languages that support object-oriented techniques have been developed. One such programming language is C++.
Two common characteristics of object-oriented programming languages are support for data encapsulation and data type inheritance. Data encapsulation refers to the binding of functions and data. Inheritance refers to the ability to declare a data type in terms of other data types.
In the C++ language, object-oriented techniques are supported through the use of classes. A class is a user-defined type. A class declaration describes the data members and function members of the class. For example, the following declaration defines the data members and the function member of a class named CIRCLE.
______________________________________ class CIRCLE {int x, y; int radius; void draw( ); }; ______________________________________
Variables x and y specify the center location of a circle and variable radius specifies the radius of the circle. These variables are referred to as data members of the class CIRCLE. The function draw is a user-defined function that draws the circle of the specified radius at the specified location. The function draw is referred to as a function member of class CIRCLE. The data members and function members of a class are bound together in that the function operates an instance of the class. An instance of a class is also called an object of the class.
In the syntax of C++, the following statement declares the objects a and b to be of type class CIRCLE.
CIRCLE a, b; PA0 a.x=2; PA0 a.y=2; PA0 a.radus=1; PA0 b.x=4; PA0 b.y=5; PA0 b.radius =2; PA0 a.draw( ); PA0 b.draw( ); PA0 CIRCLE a; PA0 CIRCLE.sub.-- FILL b; PA0 a.draw( ); PA0 b draw( );
This declaration causes the allocation of memory for the objects a and b, such an allocation is called an instance of the class. The following statements assign data to the data members of objects a and b.
The following statements are used to draw the circles defined by objects a and b.
A derived class is a class that inherits the characteristics--data members and function members--of its base classes. For example, the following derived class CIRCLE.sub.-- FILL inherits the characteristics of the base class CIRCLE.
______________________________________ class CIRCLE.sub.-- FILL : CIRCLE {int pattern; void fill( ); }; ______________________________________
This declaration specifies that class CIRCLE.sub.-- FILL includes all the data and function members that are in class CIRCLE in addition to the those data and function members introduced in the declaration of class CIRCLE.sub.-- FILL, that is, data member pattern and function member fill. In this example, class CIRCLE.sub.-- FILL would have data members x, y, radius, and pattern and function members draw and fill. Class CIRCLE.sub.-- FILL is said to "inherit" the characteristics of class CIRCLE. A class that inherits the characteristics of another class is a derived class (e.g., CIRCLE.sub.-- FILL). A class that does not inherit the characteristics of another class is a primary class (e.g., CIRCLE). A class whose characteristics are inherited by another class is a base class (e.g., CIRCLE is a base class of CIRCLE.sub.-- FILL). A derived class may inherit the characteristics of several classes, that is, a derived class may have several base classes. This is referred to as multiple inheritance.
A class may specify whether its function members are to be virtually inherited. Declaring that a function member is virtual means that the function can be overridden by a function of the same name and type in a derived class. In the following example, the function draw is declared to be virtual in classes CIRCLE and CIRCLE.sub.-- FILL.
______________________________________ class CIRCLE {int x, y; int radius; virtual void draw( ); }; class CIRCLE.sub.-- FILL : CIRCLE {int pattern; virtual void draw( ); }; ______________________________________
Continuing with the example, the following statement declares object a to be of type class CIRCLE and object b to be of type class CIRCLE.sub.-- FILL.
The following statement refers to the function draw as defined in class CIRCLE.
Whereas, the following statement refers to the function draw defined in class CIRCLE.sub.-- FILL.
Moreover, the following statements type cast object b to an object of type class CIRCLE and invoke the function draw that is defined in class CIRCLE.sub.-- FILL.
______________________________________ CIRCLE c; c.sub.-- ptr *CIRCLE; c.sub.-- ptr = &b; c.sub.-- ptr .fwdarw. draw( ); // CIRCLE.sub.-- FILL::draw( ______________________________________
Thus, the type casting preserves the call to the overriding function CIRCLE.sub.-- FILL::draw.
Although object-oriented techniques facilitate the development of complex computer programs, the resulting computer programs can be less efficient in execution speed and require more memory than a program developed without object-oriented techniques. It would be desirable to have method and system for implementing the techniques of object-oriented programming to improve the execution speed and reduce the memory requirements of the computer program.